package com.taotao.service.impl;

import com.taotao.mapper.TbContentCategoryMapper;
import com.taotao.pojo.EasyUITreeNode;
import com.taotao.pojo.TaotaoResult;
import com.taotao.pojo.TbContentCategory;
import com.taotao.pojo.TbContentCategoryExample;
import com.taotao.service.ContentCategoryService;
import org.apache.http.impl.execchain.TunnelRefusedException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * Created by caiyinli on 2018/8/22.
 */
@Service
public class ContentCategoryServiceImpl implements ContentCategoryService{

    @Autowired
    private TbContentCategoryMapper tbContentCategoryMapper;

    @Override
    public List<EasyUITreeNode> getCategoryList(Long parentId) {
        TbContentCategoryExample tbItemExample = new TbContentCategoryExample();
        TbContentCategoryExample.Criteria criteria = tbItemExample.createCriteria();
        criteria.andParentIdEqualTo(parentId);
        List<TbContentCategory> tbItemCats = tbContentCategoryMapper.selectByExample(tbItemExample);
        List<EasyUITreeNode> nodeList = new ArrayList<>();
        for(TbContentCategory cat: tbItemCats){
            EasyUITreeNode node = new EasyUITreeNode();
            node.setId(cat.getId());
            node.setState(cat.getIsParent()?"closed":"open");
            node.setText(cat.getName());
            nodeList.add(node);
        }

        return nodeList;
    }

    @Override
    public TaotaoResult insertContentCategory(Long parentId, String name) {
        TbContentCategory tbContentCategory = new TbContentCategory();
        tbContentCategory.setName(name);
        tbContentCategory.setIsParent(false);
        //1:正常 2:删除
        tbContentCategory.setStatus(1);
        tbContentCategory.setParentId(parentId);
        tbContentCategory.setSortOrder(1);
        tbContentCategory.setCreated(new Date());
        tbContentCategory.setUpdated(new Date());
        tbContentCategoryMapper.insert(tbContentCategory);

        //查看父节点的isParent是否为true,如果不是true,则改成true
        TbContentCategory parentCat = tbContentCategoryMapper.selectByPrimaryKey(parentId);
        //如果不为ture
        if(!parentCat.getIsParent()){
            parentCat.setIsParent(true);
            tbContentCategoryMapper.updateByPrimaryKeySelective(parentCat);
        }
        return TaotaoResult.ok(tbContentCategory);
    }

    @Override
    public TaotaoResult deleteContentCategory(Long parentId, Long id) {
        //判断该节点是否为父节点，如果是，则同时删除子节点
        TbContentCategory category = tbContentCategoryMapper.selectByPrimaryKey(id);
        if(category.getIsParent()){
            //递归删除子节点
            deleteByParentId(id);
        }else{
            //如果删除的是叶子节点，并且删除之后没有其他的叶子节点，需要把isParent改为false
            tbContentCategoryMapper.deleteByPrimaryKey(id);
            TbContentCategoryExample tbItemExample = new TbContentCategoryExample();
            TbContentCategoryExample.Criteria criteria = tbItemExample.createCriteria();
            criteria.andParentIdEqualTo(parentId);
            //查看该父子节点是否有叶子节点
            List<TbContentCategory> tbItemCats = tbContentCategoryMapper.selectByExample(tbItemExample);
            if(CollectionUtils.isEmpty(tbItemCats)){
                TbContentCategory parentCat = tbContentCategoryMapper.selectByPrimaryKey(parentId);
                parentCat.setIsParent(false);
                tbContentCategoryMapper.updateByPrimaryKeySelective(parentCat);
            }
        }
        return TaotaoResult.ok();
    }

    void deleteByParentId(Long id){
        tbContentCategoryMapper.deleteByPrimaryKey(id);
        TbContentCategoryExample tbItemExample = new TbContentCategoryExample();
        TbContentCategoryExample.Criteria criteria = tbItemExample.createCriteria();
        criteria.andParentIdEqualTo(id);
        //查询以该节点为父节点的子节点
        List<TbContentCategory> tbItemCats = tbContentCategoryMapper.selectByExample(tbItemExample);
        for (TbContentCategory tbItemCat : tbItemCats) {
            //查询是否为父节点
            TbContentCategory category = tbContentCategoryMapper.selectByPrimaryKey(tbItemCat.getId());
            if(category.getIsParent()){
                deleteByParentId(tbItemCat.getId());
            }else{
                tbContentCategoryMapper.deleteByPrimaryKey(tbItemCat.getId());
            }

        }
    }

    @Override
    public TaotaoResult updateContenCategory(Long id, String name) {
        TbContentCategory tbContentCategory = new TbContentCategory();
        tbContentCategory.setId(id);
        tbContentCategory.setName(name);
        tbContentCategoryMapper.updateByPrimaryKeySelective(tbContentCategory);
        return TaotaoResult.ok();
    }
}
